package com.fp.m7.client.widget;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.TextBox;

/**
 * A modified textbox that fires ValueChangedEvent while typing or pasting text.
 * 
 * @author 7
 */
public class ExtendedTextBox extends TextBox {

    public ExtendedTextBox() {
        super();
        sinkEvents(Event.ONPASTE);

        addKeyUpHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent evt) {
                ValueChangeEvent.fire(ExtendedTextBox.this, getText());
            }
        });
    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);

        if (Event.ONPASTE == DOM.eventGetType(event)) {
            Scheduler.get().scheduleDeferred(new Command() {
                @Override
                public void execute() {
                    ValueChangeEvent.fire(ExtendedTextBox.this, getText());
                }
            });
        }
    }
}